<?php
/**
 * Created by PhpStorm.
 * User: lyon
 * Date: 16-12-6
 * Time: 下午12:39
 */
class Mysql
{
	protected static $pdo;
    public static function _init()
    {
    	if (empty(self::$pdo))
    	{
    		self::$pdo = new PDO("mysql:host=127.0.0.1;dbname=weibo;charset=utf8", "root", "lfding");
    	}
    }
    public static function findOne($where, $field = '', $table='weibo_zombie')
    {
    	if (is_array($where))
    	{
    		$whe = '';
    		foreach ($where as $key => $value) {
    			$whe .= $key . '=' . '\'' . $value . '\'' . ' AND '; 
    		}
    		$whe = substr($whe, 0, -4);
    	}
    	$sql = "SELECT * FROM $table WHERE $whe";
    	$result = self::$pdo->query($sql);
    	$list = $result->fetch();
    	return $list;
    }
    public static function findAll($where, $field = '', $table='weibo_zombie')
    {
        if (is_array($where))
        {
            $whe = '';
            foreach ($where as $key => $value) {
                $whe .= $key . '=' . '\'' . $value . '\'' . ' AND ';
            }
            $whe = substr($whe, 0, -4);
        }
        $sql = "SELECT * FROM $table WHERE $whe";
        $result = self::$pdo->query($sql);
        $list = $result->fetchAll();
        return $list;
    }
    public static function upDateOne($data, $where, $table='weibo_zombie')
    {
    	if (is_array($where))
    	{
    		$whe = '';
    		foreach ($where as $key => $value) {
    			$whe .= $key . '=' . '\'' . $value . '\'' . ' AND '; 
    		}
    		$whe = substr($whe, 0, -4);
    	}
    	if(is_array($data))
    	{
    		$up = '';
    		foreach ($data as $key => $value) {
    			$up .= $key . '=' . '\'' . $value . '\'' . ' , ';  
    		}
    		$up = substr($up, 0, -3);
    	}
    	$sql = "UPDATE $table SET $up WHERE $whe";
    	if (self::$pdo->exec($sql))
        {
            return true;
        }
        else
            {
                return false;
            }
    }
}
